<?php

namespace App\Admin\Actions\UserAction;

use App\Models\User;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class AdminUserBind extends RowAction
{
    public $name = '客户绑定小二';
    
    public function handle(Model $model)
    {
	    /**
	     * @var $model User
	     */
    	
    	if ($model->admin_user_id) {
    		return $this->response()->error('该客户已绑定小二');
	    }
    	
    	DB::transaction(function () use ($model) {
		    $model->update([
			    'admin_user_id' => Admin::user()->id
		    ]);
	    });
    	
    	
	    return $this->response()->success('绑定成功')->refresh();
    }
	
	public function dialog()
	{
		$this->confirm('确定绑定当前用户吗?');
	}
}
